<?php
session_start();
if(!isset($_SESSION['username'])){
header("location:login.php");
}
?>

<html>
	<head>
		<title></title>
		<link rel="stylesheet" href="style.css">
		<script src="jquery-1.11.0.js"></script>
		<script src="gen.js"></script>
		
	<script>
	
		function syncAjax(u){
			var obj=$.ajax(	{url:u, async:false} );
			return $.parseJSON(obj.responseText);
		}


		function delete_cho(obj,id){
			var cid = document.getElementById("idcho").value;
			var u = "cho_action.php?cmd=2&id=" + id;
			var a;
			var b = confirm("Are you sure you want to delete");
			if (!b){
				return;
			}
			else{
						console.log(u);

			syncAjax(u);
			}
			deletePage();
		}	
		function deletePage(){
			var u = "cho_action.php?cmd=3";
			$(".row1").remove();
			$(".row2").remove();
			
			var a = document.getElementById("myTable");
			var head = syncAjax(u);
			for(var i = 0; i < head.cho.length; i++){
				console.log("fjkdjfkd");
				var phy = a.insertRow(i+1);
				var cell1 = phy.insertCell(0);
				var cell2 = phy.insertCell(1);
				var cell3 = phy.insertCell(2);
				var cell4 = phy.insertCell(3); 

				if(i%2==0)
					phy.className="row1";
				else
					phy.className="row2";

				cell1.innerHTML = head.cho[i].idcho;
				cell2.innerHTML = "<a href = cho_detail.php?community_id=" + head.cho[i].idcho + ">" + head.cho[i].cho_name +"</a>";
				cell3.innerHTML = "<a href = cho.php?idcho=" + head.cho[i].idcho + "> edit </a>";
				cell4.innerHTML = "<span class='hotspot' onclick='delete_cho(this," + head.cho[i].idcho + ")'>delete<span>";

			}

			}

			function getCho(id){
				var u = "cho_action.php?cmd=1&idcho="+id;
				return syncAjax(u);
			}
			function detail(obj,id){
				var r = getCho(id);

				if(r.result==0){
					$("#divStatus").text(r.message);
					return;
				}
				$("#idcho").prop("value",r.cho.idcho);
				$("#cho_name").prop("value", r.cho.cho_name);
				$("#subdistrict_id").prop("value", r.cho.subdistrict_id);
				$("#pin_number").prop("value", r.cho.pin_number);
				
				var y = event.clientY;
				
				var x = event.clientX/2;
				
				$("#divEdit").css("top",y);
				$("#divEdit").css("left",x);
				
				$("#divEdit").fadeIn(500);
				document.getElementById("save").style.display="none";
			}
			
			function edit(obj,id){

				var r = getCho(id);

				if(r.result==0){
					$("#divStatus").text(r.message);
					return;
				}
				$("#idcho").prop("value",r.cho.idcho);
				$("#cho_name").prop("value", r.cho.cho_name);
				$("#subdistrict_id").prop("value", r.cho.subdistrict_id);
				$("#pin_number").prop("value", r.cho.pin_number);
				
				var y = event.clientY;
				
				var x = event.clientX/2;
				
				$("#divEdit").css("top",y);
				$("#divEdit").css("left",x);
				
				$("#divEdit").fadeIn(500);
				//create global varial to take in idparameter
			}

			function save(){
				//complete the url

				var id =document.getElementById("idcho").value;
				//var id = 5;	
				//var id = $_REQUEST['id'];
				var choName = document.getElementById("cho_name").value;
				var subD = document.getElementById("subdistrict_id").value;
				var pinN = document.getElementById("pin_number").value;

				var u ="cho_action.php?cmd=4&id="+id+"&choName="+choName+"&subD="+subD+"&pinN="+pinN; //change the cmd to 3 to update the records
	
				r = syncAjax(u);
				// if(r.result == 1){
				// 	alert("saved");
				// }else{
				// 	alert("not saved");
				cancel();
				location.reload();
			}
			
			function saveAdd(){
					
			var choName = document.getElementById("cho_nAme").value;
			var subD = document.getElementById("sub_id").value;
			var pinN = document.getElementById("pin_num").value;
			
			var u ="cho_action.php?cmd=5&choName="+choName+"&subD="+subD+"&pinN="+pinN;
			alert(u);
			syncAjax(u);
				cancel();
				location.reload();
			}
			
			function add(){		
				var y = event.clientY;
				var x = event.clientX/2;
				
				$("#divAdd").css("top",y);
				$("#divAdd").css("left",x);
				
				$("#divAdd").fadeIn(1000);
			
			}

				function cancel(){
				//fade out the form in half a second
					$("#divAdd").fadeOut(500);
					$("#divEdit").fadeOut(500);
				}
						function saveDone(data){
				if(r.result == 1){
					alert("saved");
				}else{
					alert("not saved");
				}

			}
	</script>
</head>

	<body>
		<table >
			<tr>
				<td colspan="2" id="pageheader">
					HEALTH INFORMATION SYSTEMS
				</td>
			</tr>
			<tr>
				<td id="mainnav">
					<div class="menuitem"><a href = "cho_list.php">CHO</a></div>
					<div class="menuitem">Subdistricts</div>
					<div class="menuitem">pin numbers</div>
					<div class="menuitem" ><span onclick= 'add()'>CHO ADD</span></div> 
				
				</td>
				<td id="content">
					<div id="divPageMenu">
						
			<!--			<form cho_action="cho_lists.php" method="GET">
						<input type="text" name="cho_name" />
						<span class="menuitem"> <input type="submit" name="submit" value="Search" />
						</form>
						
						-->
					</div>
					<!--<div id="divStatus" class="status">
						Welcome to the Health Information System-->
					</div>
					<div id="divContent">
						<table class="reportTable" width="100%" id = "myTable">
							<tr class="header" >
								<td>ID</td>
								<td>CHO NAME</td>
								<td></td>
								<td></td>
							</tr>	
							
		<?php	
		
			
			$cho_name = "";
			if(isset($_REQUEST["cho_name"])){
				$community_name = $_REQUEST["cho_name"];
			}
		
		include("cho.php");
		$obj = new cho();
		if(!$obj -> search_cho($cho_name)){
		echo"error";
		exit();
		}

		   // if(!$obj -> search_community($community_name)){
			//	echo mysql_error($obj->link);
			//	exit();
			//}
			$row = $obj->fetch();
	
			if(is_null($row['cho_name'])){
				echo"<p>can not find a cho with that name</p>";
			}
			else{


	
	//$row = $obj->fetch();
	$row_counter = 0;
	while($row){
	
			
		if($row_counter%2==0){
			$style=" class='row1' ";
		}else{
			$style=" class='row2'  ";
		}
		
		$id = $row['idcho'];
		?>
		<input type = "hidden" value = <?php echo "$id"; ?> id ="idcho">
	
		<?php
		echo "<tr $style >";
		echo "<td>";
			echo $row["idcho"];
		echo "</td>";
		
		echo "<td>";
			echo "<span class='hotspot' onclick='detail(this,$id)'>" . $row["cho_name"] . "</a>";
		echo "</td>";
		
		//echo ("<td>");
				echo "<td><span class='hotspot' onclick='edit(this,$id)'>edit<span></td>";				
				echo "<td><span class='hotspot' onclick='delete_cho(this,$id)'>delete<span></td>";
				echo "</tr>";
		$row=$obj->fetch();
		$row_counter++;
	}
	
	
	//mysql_close($link);
	
	}
?>
		
						</table>
						
					</div>
				</td>
			</tr>
			</table>
			
	<div id="divEdit" class="popupForm">
		<table class="tableForm" >
					<tr>
						<td class="label">CHO Name: </td>
						<td class="field"><input type="text" value="<?php echo $row['cho_name'] ?>" id="cho_name" ></td>
					</tr>
					<tr>
					
					
					<td class="label">Subdistrict_id:</td> 
						<td class="field"><input type="text" value="<?php echo $row['subdistrict_id'] ?>" id ="subdistrict_id" >
						</td>
					</tr>
					<tr>
						<td class="label">Pin Number:</td>
						<td class="field"><input type="text" value="<?php echo $row['pin_number'] ?>" id ="pin_number" >
						</td>
					</tr>
					<tr>
						<td class="field"><input type="hidden" value="<?php echo $row['idcho'] ?>" id="idcho"></td>
					</tr>
					<tr>
						<td class="label"></td>
						<td class="field">
							<input type="button" value="save" onclick="save()" id="save">
							<input type="button" value="cancel" onclick="cancel()" >
						</td>
					</tr>
			</table>
		</div>	
			
	<div id="divAdd" class="popupForm">
		<table class="tableForm" >
					<tr>
						<td class="label">CHO Name: </td>
						<td class="field"><input type="text" value="" id="cho_nAme" ></td>
					</tr>
					<tr>
					
					
					<td class="label">Subdistrict_id:</td> 
						<td class="field"><input type="text" value="" id ="sub_id" >
						</td>
					</tr>
					<tr>
						<td class="label">Pin Number:</td>
						<td class="field"><input type="text" value="" id ="pin_num" >
						</td>
					</tr>
					<tr>
						<td class="field"><input type="hidden" value="" id="id"></td>
					</tr>
					<tr>
						<td class="label"></td>
						<td class="field">
							<input type="button" value="save" onclick="saveAdd()" >
							<input type="button" value="cancel" onclick="cancel()" >
						</td>
					</tr>
			</table>
				
	</div>		
	
	</body>
</html>	